home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / fortran.el < prev    next >
Lisp/Scheme  |  1993-06-09  |  48KB  |  1,238 lines

  1. ;;; fortran.el --- Fortran mode for GNU Emacs
  2.  
  3. ;;; Copyright (c) 1986, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Michael D. Prange <prange@erl.mit.edu>
  6. ;; Maintainer: bug-fortran-mode@erl.mit.edu
  7. ;; Version 1.30.2 (June 1, 1993)
  8. ;; Keywords: languages
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  24. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; Fortran mode has been upgraded and is now maintained by Stephen A. Wood
  29. ;; (saw@cebaf.gov).  It now will use either fixed format continuation line
  30. ;; markers (character in 6th column), or tab format continuation line style
  31. ;; (digit after a TAB character.)  A auto-fill mode has been added to
  32. ;; automatically wrap fortran lines that get too long.
  33.  
  34. ;; We acknowledge many contributions and valuable suggestions by
  35. ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
  36. ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon, 
  37. ;; Gary Sabot and Richard Stallman.
  38.  
  39. ;;; This file may be used with GNU Emacs version 18.xx if the following
  40. ;;; variable and function substitutions are made.
  41. ;;;  Replace:
  42. ;;;   frame-width                           with screen-width
  43. ;;;   auto-fill-function                    with auto-fill-hook
  44. ;;;   comment-indent-function               with comment-indent-hook
  45. ;;;   (setq unread-command-events (list c)) with (setq unread-command-char c)
  46.  
  47. ;;; Bugs to bug-fortran-mode@erl.mit.edu
  48.  
  49. (defconst fortran-mode-version "version 1.30.2")
  50.  
  51. ;;; Code:
  52.  
  53. ;;;###autoload
  54. (defvar fortran-tab-mode-default nil
  55.   "*Default tabbing/carriage control style for empty files in Fortran mode.
  56. A value of t specifies tab-digit style of continuation control.
  57. A value of nil specifies that continuation lines are marked
  58. with a character in column 6.")
  59.  
  60. ;; Buffer local, used to display mode line.
  61. (defvar fortran-tab-mode-string nil
  62.   "String to appear in mode line when TAB-format mode is on.")
  63.  
  64. (defvar fortran-do-indent 3
  65.   "*Extra indentation applied to DO blocks.")
  66.  
  67. (defvar fortran-if-indent 3
  68.   "*Extra indentation applied to IF blocks.")
  69.  
  70. (defvar fortran-structure-indent 3
  71.   "*Extra indentation applied to STRUCTURE, UNION and MAP blocks.")
  72.  
  73. (defvar fortran-continuation-indent 5
  74.   "*Extra indentation applied to Fortran continuation lines.")
  75.  
  76. (defvar fortran-comment-indent-style 'fixed
  77.   "*nil forces comment lines not to be touched,
  78. 'fixed makes fixed comment indentation to `fortran-comment-line-extra-indent'
  79. columns beyond `fortran-minimum-statement-indent-fixed' (for
  80. `indent-tabs-mode' of nil) or `fortran-minimum-statement-indent-tab' (for
  81. `indent-tabs-mode' of t), and 'relative indents to current
  82. Fortran indentation plus `fortran-comment-line-extra-indent'.")
  83.  
  84. (defvar fortran-comment-line-extra-indent 0
  85.   "*Amount of extra indentation for text within full-line comments.")
  86.  
  87. (defvar comment-line-start nil
  88.   "*Delimiter inserted to start new full-line comment.")
  89.  
  90. (defvar comment-line-start-skip nil
  91.   "*Regexp to match the start of a full-line comment.")
  92.  
  93. (defvar fortran-minimum-statement-indent-fixed 6
  94.   "*Minimum statement indentation for fixed format continuation style.")
  95.  
  96. (defvar fortran-minimum-statement-indent-tab (max tab-width 6)
  97.   "*Minimum statement indentation for TAB format continuation style.")
  98.  
  99. ;; Note that this is documented in the v18 manuals as being a string
  100. ;; of length one rather than a single character.
  101. ;; The code in this file accepts either format for compatibility.
  102. (defvar fortran-comment-indent-char " "
  103.   "*Single-character string inserted for Fortran comment indentation.
  104. Normally a space.")
  105.  
  106. (defvar fortran-line-number-indent 1
  107.   "*Maximum indentation for Fortran line numbers.
  108. 5 means right-justify them within their five-column field.")
  109.  
  110. (defvar fortran-check-all-num-for-matching-do nil
  111.   "*Non-nil causes all numbered lines to be treated as possible DO loop ends.")
  112.  
  113. (defvar fortran-blink-matching-if nil
  114.   "*From a Fortran ENDIF statement, blink the matching IF statement.")
  115.  
  116. (defvar fortran-continuation-string "$"
  117.   "*Single-character string used for Fortran continuation lines.
  118. In fixed format continuation style, this character is inserted in
  119. column 6 by \\[fortran-split-line] to begin a continuation line.
  120. Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
  121. convert the line into a continuation line of the appropriate style.
  122. Normally $.")
  123.  
  124. (defvar fortran-comment-region "c$$$"
  125.   "*String inserted by \\[fortran-comment-region]\
  126.  at start of each line in region.")
  127.  
  128. (defvar fortran-electric-line-number t
  129.   "*Non-nil causes line number digits to be moved to the correct column as\
  130.  typed.")
  131.  
  132. (defvar fortran-startup-message t
  133.   "*Non-nil displays a startup message when Fortran mode is first called.")
  134.  
  135. (defvar fortran-column-ruler-fixed
  136.   "0   4 6  10        20        30        40        5\
  137. 0        60        70\n\
  138. [   ]|{   |    |    |    |    |    |    |    |    \
  139. |    |    |    |    |}\n"
  140.   "*String displayed above current line by \\[fortran-column-ruler].
  141. This variable used in fixed-format mode.")
  142.  
  143. (defvar fortran-column-ruler-tab
  144.   "0       810        20        30        40        5\
  145. 0        60        70\n\
  146. [   ]|  { |    |    |    |    |    |    |    |    \
  147. |    |    |    |    |}\n"
  148.   "*String displayed above current line by \\[fortran-column-ruler].
  149. This variable used in TAB-format mode.")
  150.  
  151. (defconst bug-fortran-mode "bug-fortran-mode@erl.mit.edu"
  152.   "Address of mailing list for Fortran mode bugs.")
  153.  
  154. (defvar fortran-mode-syntax-table nil
  155.   "Syntax table in use in Fortran mode buffers.")
  156.  
  157. (defvar fortran-analyze-depth 100
  158.   "Number of lines to scan to determine whether to use fixed or TAB format\
  159.  style.")
  160.  
  161. (defvar fortran-break-before-delimiters t
  162.   "*Non-nil causes `fortran-do-auto-fill' to break lines before delimiters.")
  163.  
  164. (if fortran-mode-syntax-table
  165.     ()
  166.   (setq fortran-mode-syntax-table (make-syntax-table))
  167.   (modify-syntax-entry ?\; "w" fortran-mode-syntax-table)
  168.   (modify-syntax-entry ?\r " " fortran-mode-syntax-table)
  169.   (modify-syntax-entry ?+ "." fortran-mode-syntax-table)
  170.   (modify-syntax-entry ?- "." fortran-mode-syntax-table)
  171.   (modify-syntax-entry ?= "." fortran-mode-syntax-table)
  172.   (modify-syntax-entry ?* "." fortran-mode-syntax-table)
  173.   (modify-syntax-entry ?/ "." fortran-mode-syntax-table)
  174.   (modify-syntax-entry ?\' "\"" fortran-mode-syntax-table)
  175.   (modify-syntax-entry ?\" "\"" fortran-mode-syntax-table)
  176.   (modify-syntax-entry ?\\ "/" fortran-mode-syntax-table)
  177.   (modify-syntax-entry ?. "w" fortran-mode-syntax-table)
  178.   (modify-syntax-entry ?_ "w" fortran-mode-syntax-table)
  179.   (modify-syntax-entry ?\n ">" fortran-mode-syntax-table))
  180.  
  181. (defvar fortran-mode-map () 
  182.   "Keymap used in Fortran mode.")
  183. (if fortran-mode-map
  184.     ()
  185.   (setq fortran-mode-map (make-sparse-keymap))
  186.   (define-key fortran-mode-map ";" 'fortran-abbrev-start)
  187.   (define-key fortran-mode-map "\C-c;" 'fortran-comment-region)
  188.   (define-key fortran-mode-map "\e\C-a" 'beginning-of-fortran-subprogram)
  189.   (define-key fortran-mode-map "\e\C-e" 'end-of-fortran-subprogram)
  190.   (define-key fortran-mode-map "\e;" 'fortran-indent-comment)
  191.   (define-key fortran-mode-map "\e\C-h" 'mark-fortran-subprogram)
  192.   (define-key fortran-mode-map "\e\n" 'fortran-split-line)
  193.   (define-key fortran-mode-map "\n" 'fortran-indent-new-line)
  194.   (define-key fortran-mode-map "\e\C-q" 'fortran-indent-subprogram)
  195.   (define-key fortran-mode-map "\C-c\C-w" 'fortran-window-create-momentarily)
  196.   (define-key fortran-mode-map "\C-c\C-r" 'fortran-column-ruler)
  197.   (define-key fortran-mode-map "\C-c\C-p" 'fortran-previous-statement)
  198.   (define-key fortran-mode-map "\C-c\C-n" 'fortran-next-statement)
  199.   (define-key fortran-mode-map "\t" 'fortran-indent-line)
  200.   (define-key fortran-mode-map "0" 'fortran-electric-line-number)
  201.   (define-key fortran-mode-map "1" 'fortran-electric-line-number)
  202.   (define-key fortran-mode-map "2" 'fortran-electric-line-number)
  203.   (define-key fortran-mode-map "3" 'fortran-electric-line-number)
  204.   (define-key fortran-mode-map "4" 'fortran-electric-line-number)
  205.   (define-key fortran-mode-map "5" 'fortran-electric-line-number)
  206.   (define-key fortran-mode-map "6" 'fortran-electric-line-number)
  207.   (define-key fortran-mode-map "7" 'fortran-electric-line-number)
  208.   (define-key fortran-mode-map "8" 'fortran-electric-line-number)
  209.   (define-key fortran-mode-map "9" 'fortran-electric-line-number))
  210.  
  211. (defvar fortran-mode-abbrev-table nil)
  212. (if fortran-mode-abbrev-table
  213.     ()
  214.   (let ((ac abbrevs-changed))
  215.     (define-abbrev-table 'fortran-mode-abbrev-table ())
  216.     (define-abbrev fortran-mode-abbrev-table  ";au"  "automatic" nil)
  217.     (define-abbrev fortran-mode-abbrev-table  ";b"   "byte" nil)
  218.     (define-abbrev fortran-mode-abbrev-table  ";bd"  "block data" nil)
  219.     (define-abbrev fortran-mode-abbrev-table  ";ch"  "character" nil)
  220.     (define-abbrev fortran-mode-abbrev-table  ";cl"  "close" nil)
  221.     (define-abbrev fortran-mode-abbrev-table  ";c"   "continue" nil)
  222.     (define-abbrev fortran-mode-abbrev-table  ";cm"  "common" nil)
  223.     (define-abbrev fortran-mode-abbrev-table  ";cx"  "complex" nil)
  224.     (define-abbrev fortran-mode-abbrev-table  ";df"  "define" nil)
  225.     (define-abbrev fortran-mode-abbrev-table  ";di"  "dimension" nil)
  226.     (define-abbrev fortran-mode-abbrev-table  ";do"  "double" nil)
  227.     (define-abbrev fortran-mode-abbrev-table  ";dc"  "double complex" nil)
  228.     (define-abbrev fortran-mode-abbrev-table  ";dp"  "double precision" nil)
  229.     (define-abbrev fortran-mode-abbrev-table  ";dw"  "do while" nil)
  230.     (define-abbrev fortran-mode-abbrev-table  ";e"   "else" nil)
  231.     (define-abbrev fortran-mode-abbrev-table  ";ed"  "enddo" nil)
  232.     (define-abbrev fortran-mode-abbrev-table  ";el"  "elseif" nil)
  233.     (define-abbrev fortran-mode-abbrev-table  ";en"  "endif" nil)
  234.     (define-abbrev fortran-mode-abbrev-table  ";eq"  "equivalence" nil)
  235.     (define-abbrev fortran-mode-abbrev-table  ";ew"  "endwhere" nil)
  236.     (define-abbrev fortran-mode-abbrev-table  ";ex"  "external" nil)
  237.     (define-abbrev fortran-mode-abbrev-table  ";ey"  "entry" nil)
  238.     (define-abbrev fortran-mode-abbrev-table  ";f"   "format" nil)
  239.     (define-abbrev fortran-mode-abbrev-table  ";fa"  ".false." nil)
  240.     (define-abbrev fortran-mode-abbrev-table  ";fu"  "function" nil)
  241.     (define-abbrev fortran-mode-abbrev-table  ";g"   "goto" nil)
  242.     (define-abbrev fortran-mode-abbrev-table  ";im"  "implicit" nil)
  243.     (define-abbrev fortran-mode-abbrev-table  ";ib"  "implicit byte" nil)
  244.     (define-abbrev fortran-mode-abbrev-table  ";ic"  "implicit complex" nil)
  245.     (define-abbrev fortran-mode-abbrev-table  ";ich" "implicit character" nil)
  246.     (define-abbrev fortran-mode-abbrev-table  ";ii"  "implicit integer" nil)
  247.     (define-abbrev fortran-mode-abbrev-table  ";il"  "implicit logical" nil)
  248.     (define-abbrev fortran-mode-abbrev-table  ";ir"  "implicit real" nil)
  249.     (define-abbrev fortran-mode-abbrev-table  ";inc" "include" nil)
  250.     (define-abbrev fortran-mode-abbrev-table  ";in"  "integer" nil)
  251.     (define-abbrev fortran-mode-abbrev-table  ";intr" "intrinsic" nil)
  252.     (define-abbrev fortran-mode-abbrev-table  ";l"   "logical" nil)
  253.     (define-abbrev fortran-mode-abbrev-table  ";n"   "namelist" nil)
  254.     (define-abbrev fortran-mode-abbrev-table  ";o"   "open" nil) ; was ;op
  255.     (define-abbrev fortran-mode-abbrev-table  ";pa"  "parameter" nil)
  256.     (define-abbrev fortran-mode-abbrev-table  ";pr"  "program" nil)
  257.     (define-abbrev fortran-mode-abbrev-table  ";ps"  "pause" nil)
  258.     (define-abbrev fortran-mode-abbrev-table  ";p"   "print" nil)
  259.     (define-abbrev fortran-mode-abbrev-table  ";rc"  "record" nil)
  260.     (define-abbrev fortran-mode-abbrev-table  ";re"  "real" nil)
  261.     (define-abbrev fortran-mode-abbrev-table  ";r"   "read" nil)
  262.     (define-abbrev fortran-mode-abbrev-table  ";rt"  "return" nil)
  263.     (define-abbrev fortran-mode-abbrev-table  ";rw"  "rewind" nil)
  264.     (define-abbrev fortran-mode-abbrev-table  ";s"   "stop" nil)
  265.     (define-abbrev fortran-mode-abbrev-table  ";sa"  "save" nil)
  266.     (define-abbrev fortran-mode-abbrev-table  ";st"  "structure" nil)
  267.     (define-abbrev fortran-mode-abbrev-table  ";sc"  "static" nil)
  268.     (define-abbrev fortran-mode-abbrev-table  ";su"  "subroutine" nil)
  269.     (define-abbrev fortran-mode-abbrev-table  ";tr"  ".true." nil)
  270.     (define-abbrev fortran-mode-abbrev-table  ";ty"  "type" nil)
  271.     (define-abbrev fortran-mode-abbrev-table  ";vo"  "volatile" nil)
  272.     (define-abbrev fortran-mode-abbrev-table  ";w"   "write" nil)
  273.     (define-abbrev fortran-mode-abbrev-table  ";wh"  "where" nil)
  274.     (setq abbrevs-changed ac)))
  275.  
  276. ;;;###autoload
  277. (defun fortran-mode ()
  278.   "Major mode for editing Fortran code.
  279. \\[fortran-indent-line] indents the current Fortran line correctly. 
  280. DO statements must not share a common CONTINUE.
  281.  
  282. Type ;? or ;\\[help-command] to display a list of built-in\
  283.  abbrevs for Fortran keywords.
  284.  
  285. Key definitions:
  286. \\{fortran-mode-map}
  287.  
  288. Variables controlling indentation style and extra features:
  289.  
  290.  comment-start
  291.     Normally nil in Fortran mode.  If you want to use comments
  292.     starting with `!', set this to the string \"!\".
  293.  fortran-do-indent
  294.     Extra indentation within do blocks.  (default 3)
  295.  fortran-if-indent
  296.     Extra indentation within if blocks.  (default 3)
  297.  fortran-structure-indent
  298.     Extra indentation within structure, union and map blocks.  (default 3)
  299.  fortran-continuation-indent
  300.     Extra indentation applied to continuation statements.  (default 5)
  301.  fortran-comment-line-extra-indent
  302.     Amount of extra indentation for text within full-line comments. (default 0)
  303.  fortran-comment-indent-style
  304.     nil    means don't change indentation of text in full-line comments,
  305.     fixed  means indent that text at `fortran-comment-line-extra-indent' beyond
  306.            the value of `fortran-minimum-statement-indent-fixed' (for fixed
  307.            format continuation style) or `fortran-minimum-statement-indent-tab'
  308.            (for TAB format continuation style).
  309.     relative  means indent at `fortran-comment-line-extra-indent' beyond the
  310.            indentation for a line of code.
  311.     (default 'fixed)
  312.  fortran-comment-indent-char
  313.     Single-character string to be inserted instead of space for
  314.     full-line comment indentation.  (default \" \")
  315.  fortran-minimum-statement-indent-fixed
  316.     Minimum indentation for Fortran statements in fixed format mode. (def.6)
  317.  fortran-minimum-statement-indent-tab
  318.     Minimum indentation for Fortran statements in TAB format mode. (default 9)
  319.  fortran-line-number-indent
  320.     Maximum indentation for line numbers.  A line number will get
  321.     less than this much indentation if necessary to avoid reaching
  322.     column 5.  (default 1)
  323.  fortran-check-all-num-for-matching-do
  324.     Non-nil causes all numbered lines to be treated as possible \"continue\"
  325.     statements.  (default nil)
  326.  fortran-blink-matching-if 
  327.     From a Fortran ENDIF statement, blink the matching IF statement.
  328.     (default nil)
  329.  fortran-continuation-string
  330.     Single-character string to be inserted in column 5 of a continuation
  331.     line.  (default \"$\")
  332.  fortran-comment-region
  333.     String inserted by \\[fortran-comment-region] at start of each line in 
  334.     region.  (default \"c$$$\")
  335.  fortran-electric-line-number
  336.     Non-nil causes line number digits to be moved to the correct column 
  337.     as typed.  (default t)
  338.  fortran-break-before-delimiters
  339.     Non-nil causes `fortran-do-auto-fill' breaks lines before delimiters.
  340.     (default t)
  341.  fortran-startup-message
  342.     Set to nil to inhibit message first time Fortran mode is used.
  343.  
  344. Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
  345. with no args, if that value is non-nil."
  346.   (interactive)
  347.   (kill-all-local-variables)
  348.   (if fortran-startup-message
  349.       (message "Emacs Fortran mode %s.  Bugs to %s"
  350.            fortran-mode-version bug-fortran-mode))
  351.   (setq fortran-startup-message nil)
  352.   (setq local-abbrev-table fortran-mode-abbrev-table)
  353.   (set-syntax-table fortran-mode-syntax-table)
  354.   (make-local-variable 'fortran-break-before-delimiters)
  355.   (setq fortran-break-before-delimiters t)
  356.   (make-local-variable 'indent-line-function)
  357.   (setq indent-line-function 'fortran-indent-line)
  358.   (make-local-variable 'comment-indent-function)
  359.   (setq comment-indent-function 'fortran-comment-hook)
  360.   (make-local-variable 'comment-line-start-skip)
  361.   (setq comment-line-start-skip
  362.     "^[Cc*]\\(\\([^ \t\n]\\)\\2\\2*\\)?[ \t]*\\|^#.*")
  363.   (make-local-variable 'comment-line-start)
  364.   (setq comment-line-start "c")
  365.   (make-local-variable 'comment-start-skip)
  366.   (setq comment-start-skip "![ \t]*")
  367.   (make-local-variable 'comment-start)
  368.   (setq comment-start nil)
  369.   (make-local-variable 'require-final-newline)
  370.   (setq require-final-newline t)
  371.   (make-local-variable 'abbrev-all-caps)
  372.   (setq abbrev-all-caps t)
  373.   (make-local-variable 'indent-tabs-mode)
  374.   (setq indent-tabs-mode nil)
  375. ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
  376.   (setq fill-column 72) ; Already local?
  377.   (use-local-map fortran-mode-map)
  378.   (setq mode-name "Fortran")
  379.   (setq major-mode 'fortran-mode)
  380. ;;;(make-local-variable 'fortran-tab-mode)
  381.   (make-local-variable 'fortran-comment-line-extra-indent)
  382.   (make-local-variable 'fortran-minimum-statement-indent-fixed)
  383.   (make-local-variable 'fortran-minimum-statement-indent-tab)
  384.   (make-local-variable 'fortran-column-ruler-fixed)
  385.   (make-local-variable 'fortran-column-ruler-tab)
  386.   (make-local-variable 'fortran-tab-mode-string)
  387.   (setq fortran-tab-mode-string " TAB-format")
  388.   (setq indent-tabs-mode (fortran-analyze-file-format))
  389.   (run-hooks 'fortran-mode-hook))
  390.  
  391. (defun fortran-comment-hook ()
  392.   (save-excursion
  393.     (skip-chars-backward " \t")
  394.     (max (+ 1 (current-column))
  395.      comment-column)))
  396.  
  397. (defun fortran-indent-comment ()
  398.   "Align or create comment on current line.
  399. Existing comments of all types are recognized and aligned.
  400. If the line has no comment, a side-by-side comment is inserted and aligned
  401. if the value of  comment-start  is not nil.
  402. Otherwise, a separate-line comment is inserted, on this line
  403. or on a new line inserted before this line if this line is not blank."
  404.   (interactive)
  405.   (beginning-of-line)
  406.   ;; Recognize existing comments of either kind.
  407.   (cond ((looking-at comment-line-start-skip)
  408.      (fortran-indent-line))
  409.     ((fortran-find-comment-start-skip) ; catches any inline comment and
  410.                     ; leaves point after comment-start-skip
  411.      (if comment-start-skip
  412.          (progn (goto-char (match-beginning 0))
  413.             (if (not (= (current-column) (fortran-comment-hook)))
  414.             (progn (delete-horizontal-space)
  415.                    (indent-to (fortran-comment-hook)))))
  416.        (end-of-line)))        ; otherwise goto end of line or sth else?
  417.     ;; No existing comment.
  418.     ;; If side-by-side comments are defined, insert one,
  419.     ;; unless line is now blank.
  420.     ((and comment-start (not (looking-at "^[ \t]*$")))
  421.      (end-of-line)
  422.      (delete-horizontal-space)
  423.      (indent-to (fortran-comment-hook))
  424.      (insert comment-start))
  425.     ;; Else insert separate-line comment, making a new line if nec.
  426.     (t
  427.      (if (looking-at "^[ \t]*$")
  428.          (delete-horizontal-space)
  429.        (beginning-of-line)
  430.        (insert "\n")
  431.        (forward-char -1))
  432.      (insert comment-line-start)
  433.      (insert-char (if (stringp fortran-comment-indent-char)
  434.               (aref fortran-comment-indent-char 0)
  435.             fortran-comment-indent-char)
  436.               (- (calculate-fortran-indent) (current-column))))))
  437.  
  438. (defun fortran-comment-region (beg-region end-region arg)
  439.   "Comments every line in the region.
  440. Puts fortran-comment-region at the beginning of every line in the region. 
  441. BEG-REGION and END-REGION are args which specify the region boundaries. 
  442. With non-nil ARG, uncomments the region."
  443.   (interactive "*r\nP")
  444.   (let ((end-region-mark (make-marker)) (save-point (point-marker)))
  445.     (set-marker end-region-mark end-region)
  446.     (goto-char beg-region)
  447.     (beginning-of-line)
  448.     (if (not arg)            ;comment the region
  449.     (progn (insert fortran-comment-region)
  450.            (while (and  (= (forward-line 1) 0)
  451.                 (< (point) end-region-mark))
  452.          (insert fortran-comment-region)))
  453.       (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
  454.     (if (looking-at com)
  455.         (delete-region (point) (match-end 0)))
  456.     (while (and  (= (forward-line 1) 0)
  457.              (< (point) end-region-mark))
  458.       (if (looking-at com)
  459.           (delete-region (point) (match-end 0))))))
  460.     (goto-char save-point)
  461.     (set-marker end-region-mark nil)
  462.     (set-marker save-point nil)))
  463.  
  464. (defun fortran-abbrev-start ()
  465.   "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs. 
  466. Any other key combination is executed normally."
  467.   (interactive)
  468.   (let (c)
  469.     (insert last-command-char)
  470.     (if (or (eq (setq c (read-event)) ??)    ;insert char if not equal to `?'
  471.         (eq c help-char))
  472.     (fortran-abbrev-help)
  473.       (setq unread-command-events (list c)))))
  474.  
  475. (defun fortran-abbrev-help ()
  476.   "List the currently defined abbrevs in Fortran mode."
  477.   (interactive)
  478.   (message "Listing abbrev table...")
  479.   (display-buffer (fortran-prepare-abbrev-list-buffer))
  480.   (message "Listing abbrev table...done"))
  481.  
  482. (defun fortran-prepare-abbrev-list-buffer ()
  483.   (save-excursion
  484.     (set-buffer (get-buffer-create "*Abbrevs*"))
  485.     (erase-buffer)
  486.     (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
  487.     (goto-char (point-min))
  488.     (set-buffer-modified-p nil)
  489.     (edit-abbrevs-mode))
  490.   (get-buffer-create "*Abbrevs*"))
  491.  
  492. (defun fortran-column-ruler ()
  493.   "Inserts a column ruler momentarily above current line, till next keystroke.
  494. The ruler is defined by the value of `fortran-column-ruler'.
  495. The key typed is executed unless it is SPC."
  496.   (interactive)
  497.   (momentary-string-display 
  498.    (if indent-tabs-mode
  499.        fortran-column-ruler-tab
  500.      fortran-column-ruler-fixed)
  501.    (save-excursion
  502.      (beginning-of-line) 
  503.      (if (eq (window-start (selected-window))
  504.          (window-point (selected-window)))
  505.      (progn (forward-line) (point))
  506.        (point)))
  507.    nil "Type SPC or any command to erase ruler."))
  508.  
  509. (defun fortran-window-create ()
  510.   "Makes the window 72 columns wide.
  511. See also `fortran-window-create-momentarily'."
  512.   (interactive)
  513.   (condition-case error
  514.       (progn
  515.     (let ((window-min-width 2))
  516.       (if (< (window-width) (frame-width))
  517.           (enlarge-window-horizontally (- (frame-width)
  518.                           (window-width) 1)))
  519.       (split-window-horizontally 73)
  520.       (other-window 1)
  521.       (switch-to-buffer " fortran-window-extra" t)
  522.       (select-window (previous-window))))
  523.     (error (message "No room for Fortran window.")
  524.        'error)))
  525.  
  526. (defun fortran-window-create-momentarily (&optional arg)
  527.   "Momentarily makes the window 72 columns wide.
  528. Optional ARG non-nil and non-unity disables the momentary feature.
  529. See also `fortran-window-create'."
  530.   (interactive "p")
  531.   (if (or (not arg)
  532.       (= arg 1))
  533.       (save-window-excursion
  534.     (if (not (equal (fortran-window-create) 'error))
  535.         (progn (message "Type SPC to continue editing.")
  536.            (let ((char (read-event)))
  537.              (or (equal char (string-to-char " "))
  538.              (setq unread-command-events (list char)))))))
  539.     (fortran-window-create)))
  540.  
  541. (defun fortran-split-line ()
  542.   "Break line at point and insert continuation marker and alignment."
  543.   (interactive)
  544.   (delete-horizontal-space)
  545.   (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip))
  546.       (insert "\n" comment-line-start " ")
  547.     (if indent-tabs-mode
  548.     (progn 
  549.       (insert "\n\t")
  550.       (insert-char (fortran-numerical-continuation-char) 1))
  551.       (insert "\n " fortran-continuation-string)));Space after \n important
  552.   (fortran-indent-line))        ;when the cont string is C, c or *.
  553.  
  554. (defun fortran-numerical-continuation-char ()
  555.   "Return a digit for tab-digit style of continuation lines.
  556. If, previous line is a tab-digit continuation line, returns that digit
  557. plus one.  Otherwise return 1.  Zero not allowed."
  558.   (save-excursion
  559.     (forward-line -1)
  560.     (if (looking-at "\t[1-9]")
  561.     (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
  562.       ?1)))
  563.  
  564. (defun delete-horizontal-regexp (chars)
  565.   "Delete all characters in CHARS around point.
  566. CHARS is like the inside of a [...] in a regular expression
  567. except that ] is never special and \ quotes ^, - or \."
  568.   (interactive "*s")
  569.   (skip-chars-backward chars)
  570.   (delete-region (point) (progn (skip-chars-forward chars) (point))))
  571.  
  572. (defun fortran-electric-line-number (arg)
  573.   "Self insert, but if part of a Fortran line number indent it automatically.
  574. Auto-indent does not happen if a numeric arg is used."
  575.   (interactive "P")
  576.   (if (or arg (not fortran-electric-line-number))
  577.       (if arg 
  578.       (self-insert-command (prefix-numeric-value arg))
  579.     (self-insert-command 1))
  580.     (if (or (and (= 5 (current-column))
  581.          (save-excursion
  582.            (beginning-of-line)
  583.            (looking-at "     ")));In col 5 with only spaces to left.
  584.         (and (= (if indent-tabs-mode
  585.           fortran-minimum-statement-indent-tab
  586.         fortran-minimum-statement-indent-fixed) (current-column))
  587.          (save-excursion
  588.            (beginning-of-line)
  589.            (looking-at "\t"));In col 8 with a single tab to the left.
  590.          (not (or (eq last-command 'fortran-indent-line)
  591.               (eq last-command
  592.                   'fortran-indent-new-line))))
  593.         (save-excursion
  594.           (re-search-backward "[^ \t0-9]"
  595.                   (save-excursion
  596.                     (beginning-of-line)
  597.                     (point))
  598.                   t)) ;not a line number
  599.         (looking-at "[0-9]")    ;within a line number
  600.         )
  601.     (self-insert-command (prefix-numeric-value arg))
  602.       (skip-chars-backward " \t")
  603.       (insert last-command-char)
  604.       (fortran-indent-line))))
  605.  
  606. (defun beginning-of-fortran-subprogram ()
  607.   "Moves point to the beginning of the current Fortran subprogram."
  608.   (interactive)
  609.   (let ((case-fold-search t))
  610.     (beginning-of-line -1)
  611.     (re-search-backward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  612.     (if (looking-at "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")
  613.     (forward-line 1))))
  614.  
  615. (defun end-of-fortran-subprogram ()
  616.   "Moves point to the end of the current Fortran subprogram."
  617.   (interactive)
  618.   (let ((case-fold-search t))
  619.     (beginning-of-line 2)
  620.     (re-search-forward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  621.     (goto-char (match-beginning 0))
  622.     (forward-line 1)))
  623.  
  624. (defun mark-fortran-subprogram ()
  625.   "Put mark at end of Fortran subprogram, point at beginning. 
  626. The marks are pushed."
  627.   (interactive)
  628.   (end-of-fortran-subprogram)
  629.   (push-mark (point))
  630.   (beginning-of-fortran-subprogram))
  631.  
  632. (defun fortran-previous-statement ()
  633.   "Moves point to beginning of the previous Fortran statement.
  634. Returns `first-statement' if that statement is the first
  635. non-comment Fortran statement in the file, and nil otherwise."
  636.   (interactive)
  637.   (let (not-first-statement continue-test)
  638.     (beginning-of-line)
  639.     (setq continue-test
  640.       (and
  641.        (not (looking-at comment-line-start-skip))
  642.        (or (looking-at
  643.             (concat "[ \t]*" (regexp-quote fortran-continuation-string)))
  644.            (or (looking-at "     [^ 0\n]")
  645.            (looking-at "\t[1-9]")))))
  646.     (while (and (setq not-first-statement (= (forward-line -1) 0))
  647.         (or (looking-at comment-line-start-skip)
  648.             (looking-at "[ \t]*$")
  649.             (looking-at "     [^ 0\n]")
  650.             (looking-at "\t[1-9]")
  651.             (looking-at (concat "[ \t]*"  comment-start-skip)))))
  652.     (cond ((and continue-test
  653.         (not not-first-statement))
  654.        (message "Incomplete continuation statement."))
  655.       (continue-test    
  656.        (fortran-previous-statement))
  657.       ((not not-first-statement)
  658.        'first-statement))))
  659.  
  660. (defun fortran-next-statement ()
  661.   "Moves point to beginning of the next Fortran statement.
  662. Returns `last-statement' if that statement is the last
  663. non-comment Fortran statement in the file, and nil otherwise."
  664.   (interactive)
  665.   (let (not-last-statement)
  666.     (beginning-of-line)
  667.     (while (and (setq not-last-statement
  668.               (and (= (forward-line 1) 0)
  669.                (not (eobp))))
  670.          (or (looking-at comment-line-start-skip)
  671.              (looking-at "[ \t]*$")
  672.              (looking-at "     [^ 0\n]")
  673.              (looking-at "\t[1-9]")
  674.              (looking-at (concat "[ \t]*"  comment-start-skip)))))
  675.     (if (not not-last-statement)
  676.      'last-statement)))
  677.  
  678. (defun fortran-blink-matching-if ()
  679.   "From a Fortran ENDIF statement, blink the matching IF statement."
  680.   (let ((count 1) (top-of-window (window-start)) matching-if
  681.     (endif-point (point)) message)
  682.     (if (save-excursion (beginning-of-line)
  683.             (skip-chars-forward " \t0-9")
  684.             (looking-at "end[ \t]*if\\b"))
  685.     (progn
  686.       (save-excursion
  687.         (while (and (not (= count 0))
  688.             (not (eq (fortran-previous-statement)
  689.                  'first-statement))
  690.             (not (looking-at
  691.                   "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")))
  692.                     ; Keep local to subprogram
  693.           (skip-chars-forward " \t0-9")
  694.           (cond ((looking-at "if[ \t]*(")
  695.              (save-excursion
  696.                (if (or
  697.                 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
  698.                 (let (then-test);multi-line if-then
  699.                   (while
  700.                   (and (= (forward-line 1) 0)
  701.                     ;search forward for then
  702.                        (or (looking-at "     [^ 0\n]")
  703.                        (looking-at "\t[1-9]"))
  704.                        (not
  705.                     (setq
  706.                      then-test
  707.                      (looking-at
  708.                       ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
  709.                   then-test))
  710.                (setq count (- count 1)))))
  711.             ((looking-at "end[ \t]*if\\b")
  712.              (setq count (+ count 1)))))
  713.         (if (not (= count 0))
  714.         (setq message "No matching if.")
  715.           (if (< (point) top-of-window)
  716.           (setq message (concat "Matches " (buffer-substring
  717.                             (progn (beginning-of-line)
  718.                                (point))
  719.                             (progn (end-of-line)
  720.                                (point)))))
  721.         (setq matching-if (point)))))
  722.       (if message
  723.           (message "%s" message)
  724.         (goto-char matching-if)
  725.         (sit-for 1)
  726.         (goto-char endif-point))))))
  727.  
  728. (defun fortran-indent-line ()
  729.   "Indents current Fortran line based on its contents and on previous lines."
  730.   (interactive)
  731.   (let ((cfi (calculate-fortran-indent)))
  732.     (save-excursion
  733.       (beginning-of-line)
  734.       (if (or (not (= cfi (fortran-current-line-indentation)))
  735.           (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
  736.            (not (fortran-line-number-indented-correctly-p))))
  737.       (fortran-indent-to-column cfi)
  738.     (beginning-of-line)
  739.     (if (and (not (looking-at comment-line-start-skip))
  740.          (fortran-find-comment-start-skip))
  741.         (fortran-indent-comment))))
  742.     ;; Never leave point in left margin.
  743.     (if (< (current-column) cfi)
  744.     (move-to-column cfi))
  745.     (if (and auto-fill-function
  746.          (> (save-excursion (end-of-line) (current-column)) fill-column))
  747.     (save-excursion
  748.       (end-of-line)
  749.       (fortran-do-auto-fill)))
  750.     (if fortran-blink-matching-if
  751.     (fortran-blink-matching-if))))
  752.  
  753. (defun fortran-indent-new-line ()
  754.   "Reindent the current Fortran line, insert a newline and indent the newline.
  755. An abbrev before point is expanded if `abbrev-mode' is non-nil."
  756.   (interactive)
  757.   (if abbrev-mode (expand-abbrev))
  758.   (save-excursion
  759.     (beginning-of-line)
  760.     (skip-chars-forward " \t")
  761.     (if (or (looking-at "[0-9]")    ;Reindent only where it is most
  762.         (looking-at "end")        ;likely to be necessary
  763.         (looking-at "else")
  764.         (looking-at (regexp-quote fortran-continuation-string)))
  765.     (fortran-indent-line)))
  766.   (newline)
  767.   (fortran-indent-line))
  768.  
  769. (defun fortran-indent-subprogram ()
  770.   "Properly indents the Fortran subprogram which contains point."
  771.   (interactive)
  772.   (save-excursion
  773.     (mark-fortran-subprogram)
  774.     (message "Indenting subprogram...")
  775.     (indent-region (point) (mark) nil))
  776.   (message "Indenting subprogram...done."))
  777.  
  778. (defun calculate-fortran-indent ()
  779.   "Calculates the Fortran indent column based on previous lines."
  780.   (let (icol first-statement (case-fold-search t)
  781.          (fortran-minimum-statement-indent
  782.           (if indent-tabs-mode
  783.           fortran-minimum-statement-indent-tab
  784.         fortran-minimum-statement-indent-fixed)))
  785.     (save-excursion
  786.       (setq first-statement (fortran-previous-statement))
  787.       (if first-statement
  788.       (setq icol fortran-minimum-statement-indent)
  789.     (progn
  790.       (if (= (point) (point-min))
  791.           (setq icol fortran-minimum-statement-indent)
  792.         (setq icol (fortran-current-line-indentation)))
  793.       (skip-chars-forward " \t0-9")
  794.       (cond ((looking-at "if[ \t]*(")
  795.          (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
  796.              (let (then-test)    ;multi-line if-then
  797.                (while (and (= (forward-line 1) 0)
  798.                        ;;search forward for then
  799.                        (or (looking-at "     [^ 0\n]")
  800.                        (looking-at "\t[1-9]"))
  801.                        (not (setq then-test (looking-at
  802.                                  ".*then\\b[ \t]\
  803. *[^ \t_$(=a-z0-9]")))))
  804.                then-test))
  805.              (setq icol (+ icol fortran-if-indent))))
  806.         ((looking-at "\\(else\\|elseif\\)\\b")
  807.          (setq icol (+ icol fortran-if-indent)))
  808.         ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  809.          (setq icol (+ icol fortran-if-indent)))
  810.         ((looking-at "where.*(.*)[ \t]*\n")
  811.          (setq icol (+ icol fortran-if-indent)))
  812.         ((looking-at "do\\b")
  813.          (setq icol (+ icol fortran-do-indent)))
  814.         ((looking-at
  815.           "\\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
  816.          (setq icol (+ icol fortran-structure-indent)))
  817.         ((looking-at "end\\b[ \t]*[^ \t=(a-z]")
  818.          ;; Previous END resets indent to minimum
  819.          (setq icol fortran-minimum-statement-indent))))))
  820.     (save-excursion
  821.       (beginning-of-line)
  822.       (cond ((looking-at "[ \t]*$"))
  823.         ((looking-at comment-line-start-skip)
  824.          (cond ((eq fortran-comment-indent-style 'relative)
  825.             (setq icol (+ icol fortran-comment-line-extra-indent)))
  826.            ((eq fortran-comment-indent-style 'fixed)
  827.             (setq icol (+ fortran-minimum-statement-indent
  828.                   fortran-comment-line-extra-indent))))
  829.          (setq fortran-minimum-statement-indent 0))
  830.         ((or (looking-at (concat "[ \t]*"
  831.                      (regexp-quote
  832.                       fortran-continuation-string)))
  833.          (looking-at "     [^ 0\n]")
  834.          (looking-at "\t[1-9]"))
  835.          (setq icol (+ icol fortran-continuation-indent)))
  836.         ((looking-at "[ \t]*#")    ; Check for cpp directive.
  837.          (setq fortran-minimum-statement-indent 0 icol 0))
  838.         (first-statement)
  839.         ((and fortran-check-all-num-for-matching-do
  840.           (looking-at "[ \t]*[0-9]+")
  841.           (fortran-check-for-matching-do))
  842.          (setq icol (- icol fortran-do-indent)))
  843.         (t
  844.          (skip-chars-forward " \t0-9")
  845.          (cond ((looking-at "end[ \t]*if\\b")
  846.             (setq icol (- icol fortran-if-indent)))
  847.            ((looking-at "\\(else\\|elseif\\)\\b")
  848.             (setq icol (- icol fortran-if-indent)))
  849.            ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  850.             (setq icol (- icol fortran-if-indent)))
  851.            ((looking-at "end[ \t]*where\\b")
  852.             (setq icol (- icol fortran-if-indent)))
  853.            ((and (looking-at "continue\\b")
  854.              (fortran-check-for-matching-do))
  855.             (setq icol (- icol fortran-do-indent)))
  856.            ((looking-at "end[ \t]*do\\b")
  857.             (setq icol (- icol fortran-do-indent)))
  858.            ((looking-at
  859.              "end[ \t]*\
  860. \\(structure\\|union\\|map\\)\\b[ \t]*[^ \t=(a-z]")
  861.             (setq icol (- icol fortran-structure-indent)))
  862.            ((and (looking-at "end\\b[ \t]*[^ \t=(a-z]")
  863.              (not (= icol fortran-minimum-statement-indent)))
  864.              (message "Warning: `end' not in column %d.  Probably\
  865.  an unclosed block." fortran-minimum-statement-indent))))))
  866.     (max fortran-minimum-statement-indent icol)))
  867.  
  868. (defun fortran-current-line-indentation ()
  869.   "Indentation of current line, ignoring Fortran line number or continuation.
  870. This is the column position of the first non-whitespace character
  871. aside from the line number and/or column 5/8 line-continuation character.
  872. For comment lines, returns indentation of the first
  873. non-indentation text within the comment."
  874.   (save-excursion
  875.     (beginning-of-line)
  876.     (cond ((looking-at comment-line-start-skip)
  877.        (goto-char (match-end 0))
  878.        (skip-chars-forward
  879.         (if (stringp fortran-comment-indent-char)
  880.         fortran-comment-indent-char
  881.           (char-to-string fortran-comment-indent-char))))
  882.       ((or (looking-at "     [^ 0\n]")
  883.            (looking-at "\t[1-9]"))
  884.        (goto-char (match-end 0)))
  885.       (t
  886.        ;; Move past line number.
  887.        (skip-chars-forward "[ \t0-9]");From Uli
  888.        ))
  889.     ;; Move past whitespace.
  890.     (skip-chars-forward " \t")
  891.     (current-column)))
  892.  
  893. (defun fortran-indent-to-column (col)
  894.   "Indents current line with spaces to column COL.
  895. notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
  896.           line, and this continuation character is retained on indentation;
  897.        2) If `fortran-continuation-string' is the first non-whitespace
  898.           character, this is a continuation line;
  899.        3) A non-continuation line which has a number as the first
  900.           non-whitespace character is a numbered line.
  901.        4) A TAB followed by a digit indicates a continuation line."
  902.   (save-excursion
  903.     (beginning-of-line)
  904.     (if (looking-at comment-line-start-skip)
  905.     (if fortran-comment-indent-style
  906.         (let ((char (if (stringp fortran-comment-indent-char)
  907.                 (aref fortran-comment-indent-char 0)
  908.               fortran-comment-indent-char)))
  909.           (goto-char (match-end 0))
  910.           (delete-horizontal-regexp (concat " \t" (char-to-string char)))
  911.           (insert-char char (- col (current-column)))))
  912.       (if (looking-at "\t[1-9]")
  913.       (if indent-tabs-mode
  914.           (goto-char (match-end 0))
  915.         (delete-char 2)
  916.         (insert "     ")
  917.         (insert fortran-continuation-string))
  918.     (if (looking-at "     [^ 0\n]")
  919.         (if indent-tabs-mode
  920.         (progn (delete-char 6)
  921.                (insert "\t")
  922.                (insert-char (fortran-numerical-continuation-char) 1))
  923.           (forward-char 6))
  924.       (delete-horizontal-space)
  925.       ;; Put line number in columns 0-4
  926.       ;; or put continuation character in column 5.
  927.       (cond ((eobp))
  928.         ((looking-at (regexp-quote fortran-continuation-string))
  929.          (if indent-tabs-mode
  930.              (progn
  931.                (indent-to 
  932.             (if indent-tabs-mode
  933.                 fortran-minimum-statement-indent-tab
  934.               fortran-minimum-statement-indent-fixed))
  935.                (delete-char 1)
  936.                (insert-char (fortran-numerical-continuation-char) 1))
  937.            (indent-to 5)
  938.            (forward-char 1)))
  939.         ((looking-at "[0-9]+")
  940.          (let ((extra-space (- 5 (- (match-end 0) (point)))))
  941.            (if (< extra-space 0)
  942.                (message "Warning: line number exceeds 5-digit limit.")
  943.              (indent-to (min fortran-line-number-indent extra-space))))
  944.          (skip-chars-forward "0-9")))))
  945.       ;; Point is now after any continuation character or line number.
  946.       ;; Put body of statement where specified.
  947.       (delete-horizontal-space)
  948.       (indent-to col)
  949.       ;; Indent any comment following code on the same line.
  950.       (if (and comment-start-skip
  951.            (fortran-find-comment-start-skip))
  952.       (progn (goto-char (match-beginning 0))
  953.          (if (not (= (current-column) (fortran-comment-hook)))
  954.              (progn (delete-horizontal-space)
  955.                 (indent-to (fortran-comment-hook)))))))))
  956.  
  957. (defun fortran-line-number-indented-correctly-p ()
  958.   "Return t if current line's line number is correctly indented.
  959. Do not call if there is no line number."
  960.   (save-excursion
  961.     (beginning-of-line)
  962.     (skip-chars-forward " \t")
  963.     (and (<= (current-column) fortran-line-number-indent)
  964.      (or (= (current-column) fortran-line-number-indent)
  965.          (progn (skip-chars-forward "0-9")
  966.             (= (current-column) 5))))))
  967.  
  968. (defun fortran-check-for-matching-do ()
  969.   "When called from a numbered statement, returns t if matching DO is found.
  970. Otherwise return a nil."
  971.   (let (charnum
  972.     (case-fold-search t))
  973.     (save-excursion
  974.       (beginning-of-line)
  975.       (if (looking-at "[ \t]*[0-9]+")
  976.       (progn
  977.         (skip-chars-forward " \t")
  978.         (skip-chars-forward "0") ;skip past leading zeros
  979.         (setq charnum (buffer-substring (point)
  980.                         (progn (skip-chars-forward "0-9")
  981.                            (point))))
  982.         (beginning-of-line)
  983.         (and (re-search-backward
  984.           (concat
  985.            "\\(^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]\\)\\|\\(^[ \t0-9]*do\
  986. [ \t]*0*"
  987.            charnum "\\b\\)\\|\\(^[ \t]*0*" charnum "\\b\\)")
  988.           nil t)
  989.          (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum))))))))
  990.  
  991. (defun fortran-find-comment-start-skip ()
  992.   "Move to past `comment-start-skip' found on current line.
  993. Return t if `comment-start-skip' found, nil if not."
  994. ;;; In order to move point only if comment-start-skip is found,
  995. ;;; this one uses a lot of save-excursions.  Note that re-search-forward
  996. ;;; moves point even if comment-start-skip is inside a string-constant.
  997. ;;; Some code expects certain values for match-beginning and end
  998.   (interactive)
  999.   (let ((save-match-beginning) (save-match-end))
  1000.     (if (save-excursion 
  1001.       (re-search-forward comment-start-skip
  1002.                  (save-excursion (end-of-line) (point)) t))
  1003.     (progn
  1004.       (setq save-match-beginning (match-beginning 0))
  1005.       (setq save-match-end (match-end 0))
  1006.       (if (fortran-is-in-string-p (match-beginning 0))
  1007.           (progn
  1008.         (save-excursion
  1009.           (goto-char save-match-end)
  1010.           (fortran-find-comment-start-skip)) ; recurse for rest of line
  1011.         )
  1012.         (goto-char save-match-beginning)
  1013.         (re-search-forward comment-start-skip
  1014.                    (save-excursion (end-of-line) (point)) t)
  1015.         (goto-char (match-end 0))
  1016.         t))
  1017.       nil)))
  1018.  
  1019. ;;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
  1020. ;;; Test if TAB format continuation lines work.
  1021. (defun fortran-is-in-string-p (where)
  1022.   "Return non-nil if POS (a buffer position) is inside a Fortran string,
  1023. nil else."
  1024.   (save-excursion
  1025.     (goto-char where)
  1026.     (cond
  1027.      ((bolp) nil)            ; bol is never inside a string
  1028.      ((save-excursion            ; comment lines too
  1029.     (beginning-of-line)(looking-at comment-line-start-skip)) nil)
  1030.      (t (let (;; ok, serious now. Init some local vars:
  1031.           (parse-state '(0 nil nil nil nil nil 0))
  1032.           (quoted-comment-start (if comment-start
  1033.                     (regexp-quote comment-start)))
  1034.           (not-done t)
  1035.           parse-limit
  1036.           end-of-line
  1037.           )
  1038.       ;; move to start of current statement
  1039.       (fortran-next-statement)
  1040.       (fortran-previous-statement)
  1041.       ;; now parse up to WHERE
  1042.       (while not-done
  1043.         (if (or ;; skip to next line if:
  1044.          ;; - comment line?
  1045.          (looking-at comment-line-start-skip)
  1046.          ;; - at end of line?
  1047.          (eolp)
  1048.          ;; - not in a string and after comment-start?
  1049.          (and (not (nth 3 parse-state))
  1050.               comment-start
  1051.               (equal comment-start
  1052.                  (char-to-string (preceding-char)))))
  1053.         ;; get around a bug in forward-line in versions <= 18.57
  1054.         (if (or (> (forward-line 1) 0) (eobp))
  1055.             (setq not-done nil))
  1056.           ;; else:
  1057.           ;; if we are at beginning of code line, skip any
  1058.           ;; whitespace, labels and tab continuation markers.
  1059.           (if (bolp) (skip-chars-forward " \t0-9"))
  1060.           ;; if we are in column <= 5 now, check for continuation char
  1061.           (cond ((= 5 (current-column)) (forward-char 1))
  1062.             ((and (< (current-column) 5)
  1063.               (equal fortran-continuation-string
  1064.                  (char-to-string (following-char)))
  1065.               (forward-char 1))))
  1066.           ;; find out parse-limit from here
  1067.           (setq end-of-line (save-excursion (end-of-line)(point)))
  1068.           (setq parse-limit (min where end-of-line))
  1069.           ;; parse max up to comment-start, if non-nil and in current line
  1070.           (if comment-start
  1071.           (save-excursion
  1072.             (if (re-search-forward quoted-comment-start end-of-line t)
  1073.             (setq parse-limit (min (point) parse-limit)))))
  1074.           ;; now parse if still in limits
  1075.           (if (< (point) where)
  1076.           (setq parse-state (parse-partial-sexp
  1077.                      (point) parse-limit nil nil parse-state))
  1078.         (setq not-done nil))
  1079.           ))
  1080.       ;; result is
  1081.       (nth 3 parse-state))))))
  1082.  
  1083. (defun fortran-auto-fill-mode (arg)
  1084.   "Toggle fortran-auto-fill mode.
  1085. With ARG, turn `fortran-auto-fill' mode on iff ARG is positive.
  1086. In `fortran-auto-fill' mode, inserting a space at a column beyond `fill-column'
  1087. automatically breaks the line at a previous space."
  1088.   (interactive "P")
  1089.   (prog1 (setq auto-fill-function
  1090.            (if (if (null arg)
  1091.                (not auto-fill-function)
  1092.              (> (prefix-numeric-value arg) 0))
  1093.            'fortran-indent-line
  1094.          nil))
  1095.     ;; update mode-line
  1096.     (set-buffer-modified-p (buffer-modified-p))))
  1097.  
  1098. (defun fortran-do-auto-fill ()
  1099.   (interactive)
  1100.   (let* ((opoint (point))
  1101.      (bol (save-excursion (beginning-of-line) (point)))
  1102.      (eol (save-excursion (end-of-line) (point)))
  1103.      (bos (min eol (+ bol (fortran-current-line-indentation))))
  1104.      (quote
  1105.       (save-excursion
  1106.         (goto-char bol)
  1107.         (if (looking-at comment-line-start-skip)
  1108.         nil            ; OK to break quotes on comment lines.
  1109.           (move-to-column fill-column)
  1110.           (cond ((fortran-is-in-string-p (point))
  1111.              (save-excursion (re-search-backward "[^']'[^']" bol t)
  1112.                      (if fortran-break-before-delimiters
  1113.                      (point)
  1114.                        (1+ (point)))))
  1115.             (t nil)))))
  1116.      ;;
  1117.      ;; decide where to split the line. If a position for a quoted
  1118.      ;; string was found above then use that, else break the line
  1119.      ;; before the last delimiter.
  1120.      ;; Delimiters are whitespace, commas, and operators.
  1121.      ;; Will break before a pair of *'s.
  1122.      ;;
  1123.      (fill-point
  1124.       (or quote
  1125.           (save-excursion
  1126.         (move-to-column (1+ fill-column))
  1127.         (skip-chars-backward "^ \t\n,'+-/*=)"
  1128. ;;;         (if fortran-break-before-delimiters
  1129. ;;;             "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
  1130.          )
  1131.         (if (<= (point) (1+ bos))
  1132.             (progn
  1133.               (move-to-column (1+ fill-column))
  1134. ;;;what is this doing???
  1135.               (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
  1136.               (goto-char bol))))
  1137.         (if (bolp)
  1138.             (re-search-forward "[ \t]" opoint t)
  1139.           (forward-char -1)
  1140.           (if (looking-at "'")
  1141.               (forward-char 1)
  1142.             (skip-chars-backward " \t\*")))
  1143.         (if fortran-break-before-delimiters
  1144.             (point)
  1145.           (1+ (point))))))
  1146.      )
  1147.     ;; if we are in an in-line comment, don't break unless the
  1148.     ;; line of code is longer than it should be. Otherwise
  1149.     ;; break the line at the column computed above.
  1150.     ;;
  1151.     ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
  1152.     ;; don't prevent a break.
  1153.     (if (not (or (save-excursion
  1154.            (if (and (re-search-backward comment-start-skip bol t)
  1155.                 (not (fortran-is-in-string-p (point))))
  1156.                (progn
  1157.              (skip-chars-backward " \t")
  1158.              (< (current-column) (1+ fill-column)))))
  1159.          (save-excursion
  1160.            (goto-char fill-point)
  1161.            (bolp))))
  1162.     (if (> (save-excursion
  1163.          (goto-char fill-point) (current-column))
  1164.            (1+ fill-column))
  1165.         (progn (goto-char fill-point)
  1166.            (fortran-break-line))
  1167.       (save-excursion
  1168.         (if (> (save-excursion
  1169.              (goto-char fill-point) 
  1170.              (current-column))
  1171.            (+ (calculate-fortran-indent) fortran-continuation-indent))
  1172.         (progn
  1173.           (goto-char fill-point)
  1174.           (fortran-break-line))))))
  1175.     ))
  1176. (defun fortran-break-line ()
  1177.   (let ((opoint (point))
  1178.     (bol (save-excursion (beginning-of-line) (point)))
  1179.     (eol (save-excursion (end-of-line) (point)))
  1180.     (comment-string nil))
  1181.     
  1182.     (save-excursion
  1183.       (if (and comment-start-skip (fortran-find-comment-start-skip))
  1184.       (progn
  1185.         (re-search-backward comment-start-skip bol t)
  1186.         (setq comment-string (buffer-substring (point) eol))
  1187.         (delete-region (point) eol))))
  1188. ;;; Forward line 1 really needs to go to next non white line
  1189.     (if (save-excursion (forward-line 1)
  1190.             (or (looking-at "     [^ 0\n]")
  1191.                 (looking-at "\t[1-9]")))
  1192.     (progn
  1193.       (forward-line 1)
  1194.       (delete-indentation)
  1195.       (delete-char 2)
  1196.       (delete-horizontal-space)
  1197.       (fortran-do-auto-fill))
  1198.       (fortran-split-line))
  1199.     (if comment-string
  1200.     (save-excursion
  1201.       (goto-char bol)
  1202.       (end-of-line)
  1203.       (delete-horizontal-space)
  1204.       (indent-to (fortran-comment-hook))
  1205.       (insert comment-string)))))
  1206.  
  1207. (defun fortran-analyze-file-format ()
  1208.   "Returns nil if Fixed format is used, t if TAB formatting is used.
  1209. Use `fortran-tab-mode-default' if no non-comment statements are found in the
  1210. file before the end or the first `fortran-analyze-depth' lines."
  1211.   (let ((i 0))
  1212.     (save-excursion
  1213.       (goto-char (point-min))
  1214.       (setq i 0)
  1215.       (while (not (or
  1216.            (eobp)
  1217.            (looking-at "\t")
  1218.            (looking-at "      ")
  1219.            (> i fortran-analyze-depth)))
  1220.     (forward-line)
  1221.     (setq i (1+ i)))
  1222.       (cond
  1223.        ((looking-at "\t") t)
  1224.        ((looking-at "      ") nil)
  1225.        (fortran-tab-mode-default t)
  1226.        (t nil)))))
  1227.  
  1228. (or (assq 'fortran-tab-mode-string minor-mode-alist)
  1229.     (setq minor-mode-alist (cons
  1230.                 '(fortran-tab-mode-string
  1231.                   (indent-tabs-mode fortran-tab-mode-string))
  1232.                 minor-mode-alist)))
  1233.  
  1234. (provide 'fortran)
  1235.  
  1236. ;;; fortran.el ends here
  1237.  
  1238.